package com.chengcang.api.modules.busi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chengcang.api.modules.busi.dao.BusiDemandProductSnapshotDao;
import com.chengcang.api.modules.busi.dto.BusiDemandProductSnapshotDTO;
import com.chengcang.api.modules.busi.entity.BusiDemandProductSnapshotEntity;
import com.chengcang.api.modules.busi.service.BusiDemandProductSnapshotService;
import com.chengcang.api.modules.sys.dto.SysProductDTO;
import com.chengcang.api.modules.sys.service.SysProductService;
import com.chengcang.common.service.impl.CrudServiceImpl;
import com.chengcang.common.utils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author Zhanli xy321231@163.com
 * @since 1.0.0 2023-02-20
 */
@Service
public class BusiDemandProductSnapshotServiceImpl extends CrudServiceImpl<BusiDemandProductSnapshotDao, BusiDemandProductSnapshotEntity, BusiDemandProductSnapshotDTO> implements BusiDemandProductSnapshotService {


    @Autowired
    private SysProductService sysProductService;

    @Override
    public QueryWrapper<BusiDemandProductSnapshotEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<BusiDemandProductSnapshotEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Override
    public BusiDemandProductSnapshotEntity saveDemandSnapshot(Long demandId, Long productId) {
        SysProductDTO dto = sysProductService.getDetail(productId);
        BusiDemandProductSnapshotEntity entity = ConvertUtils.sourceToTarget(dto, BusiDemandProductSnapshotEntity.class);
        entity.setId(null);
        entity.setProductId(productId);
        entity.setDemandId(demandId);
        this.insert(entity);
        return entity;
    }


}